package com.seculab.admin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

	public static String getDate(String sDate, String format, int iDay) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(sDate);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, iDay);

		return sdf.format(cal.getTime());
	}
	
	public static String getMonth(String sDate, String format, int iMonth) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(sDate);		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(cal.MONTH, iMonth);
		
		return sdf.format(cal.getTime());
	}
	
	public static String getYear(String sDate, String format, int iYear) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(sDate);		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(cal.YEAR, iYear);
		
		return sdf.format(cal.getTime());
	}	
	
	public static String getDay(String date) {
		Calendar cal = Calendar.getInstance();
		int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
		String day = null;
		
		switch (day_of_week) {
		case 1:
			day = "일요일";
			break;
		case 2:
			day = "월요일";
			break;
		case 3:
			day = "화요일";
			break;
		case 4:
			day = "수요일";
			break;
		case 5:
			day = "목요일";
			break;
		case 6:
			day = "금요일";
			break;
		case 7:
			day = "토요일";
			break;
		}
		
		return day;
	}
	
	/*private static String makeDate(String date) {
		StringBuffer buffer = new StringBuffer();
		
		int year = cal.get(cal.YEAR);
		int month = cal.get(cal.MONTH) + 1;
		int day = cal.get(cal.DATE);		
		
		buffer.append(year);
		if (month < 10)
			buffer.append("0");
		buffer.append(month);
		if (day < 10)
			buffer.append("0");
		buffer.append(day);		
		
		return buffer.toString();
	}*/

	public static void main(String args[]) throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy.MM.dd HH:mm:ss", Locale.KOREA);
		Date currentTime = new Date();
		String dTime = formatter.format(currentTime);
		
		System.out.println(getYear("20100201", "yyyyMMdd", -5));
		System.out.println(getMonth("20100201", "yyyyMMdd", -5));
		System.out.println(getDate("20100201", "yyyyMMdd", -5));
	}
}
